﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test1.MyClasses;

namespace Test1.MyClassesTest
{
    [TestClass]
    public class FileProcessTest2 : TestBase
    {
        private const string BAD_FILE_NAME = @"C:\Windows\hello.exe";

        [ClassInitialize()]
        public static void ClassInitialize(TestContext tc)
        {
            tc.WriteLine("In ClassInitialize() method");
        }

        [ClassCleanup()]
        public static void ClassCleanup()
        {
            //TODO: Clean up after all tests in class
        }

        [TestMethod]
        [Description("Check to see if a file exists")]
        [Owner("Darren")]
        [Priority(1)]
        [TestCategory("NoException")]
        [Ignore]
        public void FileNameDoestExist()
        {
            //Assert.Inconclusive();
            var fileProcessor = new FileProcess();
            bool fromCall;

            //SetGoodFileName();

            ////创建文件
            //if(!string.IsNullOrEmpty(_GoodFileName))
            //{
            //    File.AppendAllText(_GoodFileName, "some text");
            //}

            TestContext.WriteLine("checking file" + _GoodFileName);
            fromCall = fileProcessor.FileExists(_GoodFileName);

            ////删除文件
            //if(File.Exists(_GoodFileName))
            //{
            //    File.Delete(_GoodFileName);
            //}

            Assert.IsTrue(fromCall);

        }

        [TestMethod]
        [Description("Check to see if file does not exist.")]
        [Owner("Darren")]
        [Priority(2)]
        [TestCategory("NoException")]
        public void FileNameDoesNotExists()
        {
            //Assert.Inconclusive();

            var fileProcessor = new FileProcess();
            bool fromCall;

            fromCall = fileProcessor.FileExists(BAD_FILE_NAME);

            Assert.IsFalse(fromCall);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        [Description("Check for a thrown ArgumentNullException using ExpectedException")]
        [Owner("Steve")]
        [Priority(3)]
        [TestCategory("Exception")]
        public void FileNameNullOrEmpty_UsingAttribute()
        {
            //Assert.Inconclusive();
            var fileProcessor = new FileProcess();
            fileProcessor.FileExists("");
        }

        [TestMethod]
        [Description("Check for a thrown ArgumentNullException using try...catch")]
        [Owner("Steve")]
        [Priority(3)]
        [TestCategory("Exception")]
        public void FileNameNullOrEmpty_UsingTryCatch()
        {
            var fileProcessor = new FileProcess();

            try
            {
                fileProcessor.FileExists("");
            }
            catch (ArgumentNullException)
            {

                return;
            }

            Assert.Fail("fail");
        }

        [TestInitialize]
        public void TestInitialize()
        {
            TestContext.WriteLine("In TestInitialize() method");

            WriteDescription(this.GetType());

            if (TestContext.TestName.StartsWith("FileNameDoestExist"))
            {
                SetGoodFileName();
                if (!string.IsNullOrEmpty(_GoodFileName))
                {
                    TestContext.WriteLine("Creating File: " + _GoodFileName);
                    File.AppendAllText(_GoodFileName, "some text");
                }
            }
        }

        [TestCleanup]
        public void TestCleanup()
        {
            TestContext.WriteLine("In TestCleanup() method");
            if (TestContext.TestName.StartsWith("FileNameDoestExist"))
            {
                if (File.Exists(_GoodFileName))
                {
                    TestContext.WriteLine("Deleting File: " + _GoodFileName);
                    File.Delete(_GoodFileName);
                }
            }
        }

        //[TestMethod]
        //[Timeout(3000)]
        //public void SimulateTimeout()
        //{
        //    System.Threading.Thread.Sleep(4000);
        //}

        [TestMethod]
        [DataRow(1, 1, DisplayName = "First Test(1,1)")]
        [DataRow(2, 2, DisplayName = "Second Test(2,2)")]
        public void AreNumbersEqual(int num1, int num2)
        {
            Assert.AreEqual(num1, num2);
        }

        [TestMethod]
        [DeploymentItem("FileToDeploy.txt")]
        [DataRow(@"C:\Windows\Regedit.exe", DisplayName = "Regedit.exe")]
        [DataRow("FileToDeploy.txt", DisplayName = "Deployment Item: FileToDeployement")]
        public void FileNameUsingDataRow(string fileName)
        {
            FileProcess fp = new FileProcess();
            bool fromCall;
            if (!fileName.Contains(@"\"))
            {
                fileName = TestContext.DeploymentDirectory + @"\" + fileName;
            }

            TestContext.WriteLine("Checking File " + fileName);
            fromCall = fp.FileExists(fileName);
            Assert.IsTrue(fromCall);
        }

        [TestMethod]
        public void FileNameDoesExistSimpleMessage()
        {
            FileProcess fp = new FileProcess();
            bool fromCall;
            fromCall = fp.FileExists(_GoodFileName);

            Assert.IsFalse(fromCall, "File " + _GoodFileName + " Does Not Exist.");
        }

        [TestMethod]
        public void AreEqualTest()
        {
            string str1 = "hi";
            string str2 = "Hi";
            Assert.AreEqual(str1, str2, true);
        }

        [TestMethod]
        public void AreSameTest()
        {
            FileProcess x = new FileProcess();
            FileProcess y = new FileProcess();

            Assert.AreSame(x, y);
        }
    }
}
